﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Cocon90.Lib.Util.Format
{
    /// <summary>
    /// 图片格式转换辅助类
    /// </summary>
    public class imageFormat
    {
        /// <summary>
        /// 将Bmp格式图片文件转为Jpeg格式图片
        /// </summary>
        /// <param name="bmpPath"></param>
        /// <param name="jpegPath"></param>
        public static void BmpToJpeg(string bmpPath, string targetJpegPath)
        {
            lock (bmpPath)
            {
                string path = targetJpegPath.Substring(0, targetJpegPath.LastIndexOf('\\') + 1);
                if (!Directory.Exists(path)) { Directory.CreateDirectory(path); }
                var img = System.Drawing.Image.FromFile(bmpPath);
                img.Save(targetJpegPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                img.Dispose();
            }
        }
        /// <summary>
        /// 将Bmp格式图片文件转为同目录下同名的Jpeg格式图片并反回路径
        /// </summary>
        public static string BmpToJpeg(string bmpPath)
        {
            lock (bmpPath)
            {
                string path = bmpPath.Substring(0, bmpPath.LastIndexOf('.')) + ".jpg";
                BmpToJpeg(bmpPath, path);
                return path;
            }
        }
    }
   
}
